//
//  MessageReceiveCell.m
//  SouthChina
//
//  Created by Mr.Y on 14/12/9.
//  Copyright (c) 2014年 科创. All rights reserved.
//




#import "MessageReceiveCell.h"
#import "ContactDao.h"
@implementation MessageReceiveCell
{
    
    BOOL isShwoTimeLable;
    BOOL isShowAudioFlag;
    
}

@synthesize  message;
@synthesize  msgSession;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        _timeLabel =[[UILabel alloc]initWithFrame:CGRectZero];
        _headImage =[[UIImageView alloc]initWithFrame:CGRectZero];
        _audioFlag =[[UIImageView alloc]initWithFrame:CGRectZero];
        _bubbleImageView =[[UIImageView alloc]initWithFrame:CGRectZero];
        _imageContentView =[[UIImageView alloc]initWithFrame:CGRectZero];
         _textContentLable =[[M80AttributedLabel alloc]initWithFrame:CGRectZero];
        _textContentLable.backgroundColor=[UIColor clearColor];

        _textContentLable.lineSpacing = 5.0;
        _textContentLable.font =[UIFont systemFontOfSize:TEXT_CONTENT_FONT];

        _nameLabel =[[UILabel alloc]initWithFrame:CGRectZero];
        [_nameLabel setFont:[UIFont systemFontOfSize:10]];
        _nameLabel.textAlignment =NSTextAlignmentCenter;

        _headImage.layer.masksToBounds = YES;
        _headImage.layer.cornerRadius = 8.0;
        
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        [_timeLabel setBackgroundColor:[UIColor clearColor]];
        [_timeLabel setFont:[UIFont systemFontOfSize:TIME_CONTENT_FONT]];
        
        
        [self.contentView addSubview:_timeLabel];
        [self.contentView addSubview:_headImage];
        
        [self.contentView addSubview:_bubbleImageView];
        [_bubbleImageView addSubview:_audioFlag];
        [_bubbleImageView addSubview:_imageContentView];
        [_bubbleImageView addSubview:_textContentLable];
        [self.contentView addSubview:_nameLabel];

        
         [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.timeLabel.text =[self.message.upate_time stringFromDate];
   
    self.timeLabel.frame = CGRectMake(0, CELL_COM_GAP, SCREEN_WIDTH, TIME_LABEL_HEGIHT);
    self.headImage.frame = CGRectMake(CELL_COM_GAP, 2*CELL_COM_GAP+(isShwoTimeLable?TIME_LABEL_HEGIHT:0), HEAD_IMG_SIZE, HEAD_IMG_SIZE);
    self.nameLabel.frame = CGRectMake(CELL_COM_GAP, 2*CELL_COM_GAP+(isShwoTimeLable?TIME_LABEL_HEGIHT:0)-15, HEAD_IMG_SIZE, 15);

    int msgConentWidth = 0;
    int msgConentHeight = 0;
    
    if ([MEDIATYPE_PICTURE isEqualToString:message.mediaType]) {
        msgConentWidth = PICTURE_CELL_WIDTH+4;
        msgConentHeight = PICTURE_CELL_HEIGTH;
 
        self.imageContentView.frame = CGRectMake(12,8,PICTURE_CELL_WIDTH-16, PICTURE_CELL_HEIGTH-16);

        self.imageContentView.contentMode = UIViewContentModeScaleAspectFit;

        NSString *url = [message getValue:@"url"];
        NSURL *urll = [NSURL URLWithString: [url hasPrefix:@"http"]?url:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,url]];
        [self.imageContentView setImageWithURL:urll placeholderImage:[UIImage imageNamed:@"empty_photo"]];

    }
    
    else{
        NSString * textShow = @"";
        if ([MEDIATYPE_AUDIO isEqualToString:self.message.mediaType]) {
            textShow = [NSString stringWithFormat:@" %@秒",self.message.length];
        }else{
            textShow = self.message.text;
        }
        
        [BaseTableCell addCoreTextMessage:textShow label:self.textContentLable];
        CGSize contentSize = [self.textContentLable sizeThatFits:CGSizeMake(MAX_CONTENT_WIDTH, CGFLOAT_MAX)];
        msgConentWidth =contentSize.width + (isShowAudioFlag?2:1)*CELL_COM_GAP+TEXT_BUBBLE_GAP_RGIHT+(isShowAudioFlag?SOUNDFLAG_IMG_SIZE:0);
        //由于两种高度技术方式有误差，sizeWithFont的计算方式高度偏高，固减去一部分
        msgConentHeight =contentSize.height + TEXT_BUBBLE_GAP*2 ;
 
    
        self.textContentLable.numberOfLines = 0;//表示label可以多行显示
        self.textContentLable.lineBreakMode = NSLineBreakByWordWrapping;//换行模式，与上面的计算保持一致。
        self.textContentLable.frame = CGRectMake(TEXT_BUBBLE_GAP_RGIHT, TEXT_BUBBLE_GAP,contentSize.width, contentSize.height);
        
        self.audioFlag.frame =  CGRectMake(msgConentWidth-CELL_COM_GAP-SOUNDFLAG_IMG_SIZE, TEXT_BUBBLE_GAP, SOUNDFLAG_IMG_SIZE, SOUNDFLAG_IMG_SIZE);
        [self.audioFlag setImage:[UIImage imageNamed:@"message_flag_sound_left"]];
        self.audioFlag.contentMode = UIViewContentModeScaleAspectFit;
        

    }
 
    self.bubbleImageView.frame = CGRectMake(3*CELL_COM_GAP+HEAD_IMG_SIZE,isShwoTimeLable?(TIME_LABEL_HEGIHT+2*CELL_COM_GAP):CELL_COM_GAP, msgConentWidth, msgConentHeight);
    [self.bubbleImageView setImage:[[UIImage imageNamed:@"message_left"]stretchableImageWithLeftCapWidth:20 topCapHeight:20]];

    NSURL *url = [NSURL URLWithString: self.msgSession.sessionPic];
    
    if ([MESSAGE_TYPE_CMD isEqualToString:msgSession.sessionType]) {
        [self.headImage setImage:[UIImage imageNamed:@"system_message_icon"]];
        self.headImage.layer.masksToBounds = YES;
        self.headImage.layer.cornerRadius = 20;
        self.nameLabel.hidden=YES;

    }
    else if ([MESSAGE_TYPE_P2G isEqualToString:msgSession.sessionType])
    {
        self.nameLabel.hidden=NO;
        [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
        NSURL *url2 ;
        //TOOD 该方法比较耗时....
        NSDictionary * sendIfo = [[msgSession getValue:@"grupContacts"] objectForKey:message.senderId];
        if (!sendIfo) {
            Contact *conn = [ContactDao getContactById:message.senderId];
            url2 = [NSURL URLWithString: conn.mHEAD_IMG];
            self.nameLabel.text=conn.mNAME;
 
        }else{
            url2 = [NSURL URLWithString: [sendIfo objectForKey:@"senderHeadImg"]];
            self.nameLabel.text=[sendIfo objectForKey:@"senderName"];
        }
        [self.headImage setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"empty_photo"]];

    }
    else{
        [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
        self.nameLabel.hidden=YES;
    }
    
    self.timeLabel.hidden = !isShwoTimeLable;
    self.audioFlag.hidden = !isShowAudioFlag;

    
}

-(void) setMsgCellValue:(Message*)msg preMessage:(Message*)preMessage session:(MessageSession*)session{
    
    self.message = msg;
    self.msgSession =session;
    if (preMessage) {
        NSTimeInterval secondsBetweenDates= [msg.upate_time timeIntervalSinceDate:preMessage.upate_time];
        isShwoTimeLable = secondsBetweenDates>=30.0f;
    }else{
        isShwoTimeLable =YES;
    }
    
    if ([MEDIATYPE_AUDIO isEqualToString:msg.mediaType]) {
        isShowAudioFlag = YES;
    }
    else{
        isShowAudioFlag=NO;
    }
    
    isShowAudioFlag = [msg.mediaType isEqualToString:MEDIATYPE_AUDIO];
    
}

@end